﻿using System;
using System.Globalization;
using System.Linq;
using Reviewer.Business;
using Reviewer.Entities;
using Reviewer.Entities.Services;

public partial class V2Controls_GameReleaseList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int month;
            if (int.TryParse(Request["m"], out month))
            {
                if (month < 1 || month > 12)
                    month = DateTime.Now.Month;
            }
            else
                month = DateTime.Now.Month;

            int year;
            if (int.TryParse(Request["y"], out year))
            {
                if (year < 1990 || year > 2100)
                    year = DateTime.Now.Year;
            }
            else
                year = DateTime.Now.Year;

            int quater = ((month - 1) / 3) + 1;

            this.uiHlPreviousMonth.NavigateUrl = Request.Url.AbsolutePath + "?m=" + (month - 1 < 1 ? 12 : month - 1).ToString() + "&y=" + (month == 1 ? year - 1 : year);
            this.uiHlNextMonth.NavigateUrl = Request.Url.AbsolutePath + "?m=" + (month + 1 > 12 ? 1 : month + 1).ToString() + "&y=" + (month == 12 ? year + 1 : year);

            CultureInfo daDK = new CultureInfo("da-DK");
            this.uiLitMonthName.Text = daDK.DateTimeFormat.GetMonthName(month);
            this.uiLitMonthYear.Text = this.uiLitQuaterYear.Text = year.ToString();
            this.uiLitQuater.Text = quater.ToString();
            this.uiLitYear.Text = year.ToString();

            DateTime[] monthDates = new DateTime[12];
            for (int i = 1; i <= 12; i++)
            {
                monthDates[i - 1] = new DateTime(year, i, 1);
            }
            this.uiRepMonthNames.DataSource = monthDates;
            this.uiRepMonthNames.DataBind();

            using (IGameService gameService = ServiceFactory.GameService)
            {
                Game[] foundGames = gameService.FindGamesByReleaseDate(month, year);

                this.uiRepMonthItems.DataSource = foundGames
                    .Where(t => t.ReleaseDateFormat.Equals(1))
                    .OrderBy(t => t.ReleaseDateFormat)
                    .ThenBy(t => t.ReleaseDate.Day)
                    .GroupBy(t => t.GameId)
                    .Union(
                        foundGames.Where(t => t.ReleaseDateFormat.Equals(2))
                        .OrderBy(t => t.Title)
                        .GroupBy(t => t.GameId)
                    );

                this.uiRepMonthItems.DataBind();
                this.uiPlnNotMonth.Visible = !(this.uiRepMonthItems.Items.Count > 0);

                this.uiRepQuaterItems.DataSource = foundGames.Where(t => t.ReleaseDateFormat.Equals(3)).OrderBy(t => t.ReleaseDateFormat).GroupBy(t => t.GameId);
                this.uiRepQuaterItems.DataBind();
                this.uiPlhQuater.Visible = this.uiRepQuaterItems.Items.Count > 0;

                this.uiRepYearItems.DataSource = foundGames.Where(t => t.ReleaseDateFormat.Equals(4)).OrderBy(t => t.ReleaseDateFormat).GroupBy(t => t.GameId);
                this.uiRepYearItems.DataBind();
                this.uiPlhYear.Visible = this.uiRepYearItems.Items.Count > 0;

            }
        }
    }

    protected string CapatalizeFirstLetter(string value)
    {
        if (value.Length >= 2)
        {
            return value.Substring(0, 1).ToUpper() + value.Substring(1);
        }
        return value;
    }

    public Game GetFirstGameFromGroup(object entity)
    {
        return (entity as IGrouping<Guid, Game>).First();
    }

    public String GetFormatName(object id)
    {
        if (id is IGrouping<String, Game>)
        {
            string formatName = String.Empty;

            using (IMetadataService<Format> service = ServiceFactory.GetMetadataService<Format>())
            {
                foreach (Game g in (id as IGrouping<String, Game>))
                {
                    Format format = service.GetAll().FirstOrDefault(t => t.Id.Equals(g.Format));
                    if (format != null)
                    {
                        formatName += (String.Empty.Equals(formatName) ? "" : "<br />") + format.Name;
                    }
                }
            }
            return formatName;
        }
        else
        {
            using (IMetadataService<Format> service = ServiceFactory.GetMetadataService<Format>())
            {
                Format format = service.GetAll().FirstOrDefault(t => t.Id.Equals(Convert.ToInt32(id)));
                if (format != null)
                {
                    return format.Name;
                }
            }
        }
        return "Ukendt format";
    }

    public String GetGenreName(object id)
    {
        using (IMetadataService<Genre> service = ServiceFactory.GetMetadataService<Genre>())
        {
            Genre genre = service.GetAll().FirstOrDefault(t => t.Id.Equals(Convert.ToInt32(id)));
            if (genre != null)
            {
                return genre.Name;
            }
        }

        return "Ukendt genre";
    }

    public Game GetFirstFromGroup(object entity)
    {
        return (entity as IGrouping<String, Game>).First();
    }

}